package com.ettractions.kiosk.services
{
	import com.ettractions.kiosk.events.KioskDataServiceEvent;
	import com.ettractions.kiosk.vo.AdVO;
	import com.ettractions.kiosk.vo.BrochureMediaVO;
	import com.ettractions.kiosk.vo.BrochureVO;
	import com.ettractions.kiosk.vo.CategoryVO;
	import com.ettractions.kiosk.vo.ContactInformationVO;
	import com.ettractions.kiosk.vo.KioskVO;
	import com.ettractions.kiosk.vo.LocationVO;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.robotlegs.mvcs.Actor;
	
	public class KioskDataService extends Actor
	{
		private static const KIOSK_XML_PATH:String = "xml/Kiosk_Jan25_v2.xml";
		
		private static const ASSETS_XML_PATH:String = "xml/Asset_Jan25_v2.xml";
		
		private static const ASSET_PREFIX:String = ".";
		
		private var _data:KioskVO;		
		
		private var _loader:URLLoader;
		
		public function load():void
		{
			_loader = new URLLoader(new URLRequest(KIOSK_XML_PATH));			
			_loader.addEventListener(Event.COMPLETE, handleKioskDataLoaded);
		}
		
		private function handleKioskDataLoaded(event:Event):void
		{
			var xml:XML = XML(_loader.data);
			var categoryNodes:XMLList = xml.Categories.PrimaryCategory;
			var adNodes:XMLList = xml.Kiosk.Ad.Advertisement;
			
			_data = new KioskVO(
				xml.Kiosk.Network.@Title,
				new LocationVO(
					null, null, null, null,
					Number(xml.Kiosk.Location.@Latitude),
					Number(xml.Kiosk.Location.@Longitude)
				)
			);
			
			for each (var categoryNode:XML in categoryNodes)
				_data.categories.push(new CategoryVO(String(categoryNode.@ID), String(categoryNode.@Name)));			
			
			for each (var adNode:XML in adNodes)
				_data.ads.push(new AdVO(String(adNode.@ID), String(adNode.@ImagePath)));

			_loader.removeEventListener(Event.COMPLETE, handleKioskDataLoaded);
			_loader.addEventListener(Event.COMPLETE, handleAssetsDataLoaded);
			_loader.load(new URLRequest(ASSETS_XML_PATH));
		}
		
		private function handleAssetsDataLoaded(event:Event):void
		{
			var xml:XML = XML(_loader.data);
			var brochureNodes:XMLList = xml.Brochures.Asset;
			
			_loader.removeEventListener(Event.COMPLETE, handleAssetsDataLoaded);
			
			// TEMP
			for (var x:uint=0; x<21; x++)
				
			for each (var brochureNode:XML in brochureNodes)
			{
				var categoryNodes:XMLList = brochureNode.Categories.PrimaryCategory;				
				var imageNodes:XMLList = brochureNode.Media.ContentImages;
				var brochure:BrochureVO = new BrochureVO(
					brochureNode.@ID,
					brochureNode.@Name,
					brochureNode.ProfileCallOut.@Text,
					brochureNode.FullSummary,
					brochureNode.Cost,
					new BrochureMediaVO(
						ASSET_PREFIX + brochureNode.Media.BrochureThumbnail.@ImagePath,
						ASSET_PREFIX + brochureNode.Media.MobileTag.@ImagePath,
						ASSET_PREFIX + brochureNode.Media.SpecialInfo.@ImagePath,
						ASSET_PREFIX + brochureNode.Media.Video.@VideoPath
					),
					new ContactInformationVO(
						brochureNode.Phone,
						brochureNode.Fax,
						brochureNode.Website,
						brochureNode.Email
					),
					new LocationVO(
						brochureNode.Address,
						brochureNode.City,
						brochureNode.State,
						brochureNode.ZipCode,						
						Number(brochureNode.Map.Location.@Latitude),
						Number(brochureNode.Map.Location.@Longitude)
					)
				);	
				
				for each (var categoryNode:XML in categoryNodes)
					for each (var category:CategoryVO in _data.categories)
						if (String(categoryNode.@ID) == category.id)
							brochure.categories.push(category);
				
				for each (var imageNode:XML in imageNodes)
					brochure.media.contentImagePaths.push(ASSET_PREFIX + imageNode.@ImagePath);
				
				_data.brochures.push(brochure);
			}
			
			// TEMP
			_data.brochures.push(brochure);
			
			eventDispatcher.dispatchEvent(new KioskDataServiceEvent(KioskDataServiceEvent.KIOSK_DATA_LOADED, _data));
		}
	}
}